<?php
namespace app\mobile\controller;

use app\model\Theme; 
use app\model\Worker; 

class WReport extends Base
{
    public function W_report()
    {
global $_G, $_P;
$fans = $this->checkuser();
$themeid = intval($_P['themeid']);
if($themeid < 0){
	return_msg('管理后台地址不正确');
}
$system=$_G['system'];
$theme = Theme::getThemeById($themeid);
$config = 	Theme::getConfig($themeid);
if(empty($theme['id'])){
	return_msg('管理后台地址不正确');
}
$myworker = Worker::getThemeWorkerByOpenidNew($_G['openid'],$themeid);
if(!($system['ruanjian']!=3 || $config['showshop']==0 || ($system['ruanjian']==3 && $config['showshop']==$myworker['company']))){
	$myworker = "";
}
if (empty($myworker)) {
	return_msg('您不是员工,无权访问');
}
$msg = Worker::gethoutaistats();
if (!empty($msg)) {
	return_msg($msg);
}

//获取每天的开始时间
// if (date("H")>=intval($config['todaytime'])){
// 	$todaytime = date("Y-m-d") . " " . intval($config['todaytime']) . ":00:00";
// }else{
// 	$todaytime = date("Y-m-d",strtotime("-1 day")) . " " . intval($config['todaytime']) . ":00:00";
// }

$todaytime = strtotime(date("Y-m-d 00:00:00"));


$do = 'myreport';
if ($system['ruanjian']==3) {
	if (!empty($myworker['company'])) {
		$shop=db_find("SELECT * FROM ".tablename(t_shangjia)." WHERE id= '{$myworker['company']}'");
		$shopname=$shop['name'] ." - ";
	}
}
if ($myworker['level'] != '3' || ($myworker['level'] == '3' && $config['isreport']==1)){

	if ($config['showalldata']==0) {
		$noalldata = " and w.themeid =" .$themeid;
	}

	if ($system['ruanjian']==3) {
		$sql = 'SELECT t.id as tid, t.name as teamname FROM ' . tablename ( t_team ) . " t
				WHERE t.company = :company 
				ORDER BY t.id  ";
		$team = db_select($sql, array(":company"=>$myworker['company']));
	}else{
		$sql = 'SELECT t.id as tid, t.name as teamname FROM ' . tablename ( t_team ) . " t
				ORDER BY t.id  ";
		$team = db_select($sql );
	}

	foreach ($team as $index => $item) {
		$myteam = db_select("SELECT r.riqi, sum(r.newfans) as newfans,sum(r.record) as record,sum(r.recordxx) as recordxx,sum(r.recordzx) as recordzx,sum(r.fee) as fee,sum(r.scansum) as scansum,sum(r.forward) as forward, sum(r.refundfee) as refundfee,sum(r.refund) as refund FROM " . tablename ( t_worker ) . " w
		left join " . tablename ( t_workerreport ) . " r on w.uid = r.workerid and w.themeid = r.themeid
		WHERE 1=1 " . $noalldata ." and w.team = " . $item['tid'] ." group by r.riqi order by riqi desc" );
		$team[$index]['list'] = $myteam;

	}

	$mycompany = db_select("SELECT r.riqi, sum(r.newfans) as newfans,sum(r.record) as record,sum(r.recordxx) as recordxx,sum(r.recordzx) as recordzx,sum(r.fee) as fee,sum(r.scansum) as scansum,sum(r.forward) as forward, sum(r.refundfee) as refundfee,sum(r.refund) as refund FROM " . tablename ( t_worker ) . " w
		left join " . tablename ( t_workerreport ) . " r on w.uid = r.workerid and w.themeid = r.themeid
		WHERE 1=1 " . $noalldata ." and w.company = " . $myworker['company'] ." group by r.riqi order by riqi desc" );

	if ($system['ruanjian']==3 && $myworker['level'] == '1') {
		$allcompany = db_select("SELECT r.riqi, sum(r.newfans) as newfans,sum(r.record) as record,sum(r.recordxx) as recordxx,sum(r.recordzx) as recordzx,sum(r.fee) as fee,sum(r.scansum) as scansum,sum(r.forward) as forward, sum(r.refundfee) as refundfee,sum(r.refund) as refund FROM " . tablename ( t_worker ) . " w
		left join " . tablename ( t_workerreport ) . " r on w.uid = r.workerid and w.themeid = r.themeid
		WHERE 1=1 " . $noalldata ." group by r.riqi order by riqi desc" );
	}

	$notworker = db_fetchcolumn ( 'SELECT COUNT(*) FROM ' . tablename ( t_record ) . "
		WHERE status > 0  and status!=3 and workerid = '' and themeid = '{$themeid}' " );
}
if($_G['companyname'] =='新飞驰软件'){
	$return_clicent = get_defined_vars();
return view('/fcworker/fc_report' , $return_clicent);
}else{
	$return_clicent = get_defined_vars();
return view('/yxworker/w_report' , $return_clicent);
}
}
}